;;; +UI.el --- (& window frame mode-line theme) -*- lexical-binding: t -*-

;;; Commentary:

;;; Code:

;; -------- diminish bars --------
(when (fboundp 'menu-bar-mode)
  (menu-bar-mode -1))
(when (fboundp 'tool-bar-mode)
  (tool-bar-mode -1))
(when (fboundp 'scroll-bar-mode) ;; 水平滾動條
  (scroll-bar-mode -1))
(when (fboundp 'horizontal-scroll-bar-mode) ;; 垂直滾動條
  (horizontal-scroll-bar-mode -1))

;; -------- split window right/below --------
;; via emacs-china.org/t/topic/945/2
(defun cor5-split-window-right ()
  "Split window with another buffer."
  (interactive)
  (select-window (split-window-right))
  (switch-to-buffer (other-buffer)))
(global-set-key (kbd "C-x 3") #'cor5-split-window-right)

(defun cor5-split-window-below ()
  "Split window with another buffer."
  (interactive)
  (select-window (split-window-below))
  (switch-to-buffer (other-buffer)))
(global-set-key (kbd "C-x 2") #'cor5-split-window-below)

;; -------- set frame size --------
(defun cor5-cut-feet-fit-framesize ()
  "Frame size be maximum (mainly for Win)."
  (interactive)
  (set-frame-size (selected-frame)
                  ;; (- (x-display-pixel-width) 18)
                  (x-display-pixel-width)
                  (- (x-display-pixel-height) 40)
                  t)
  (set-frame-position (selected-frame) 0 0))

(defun cor5-medium-frame-size ()
  "Frame size be medium."
  (interactive)
  (set-frame-size (selected-frame)
                  (round (* (x-display-pixel-width) 0.5))
                  (round (* (x-display-pixel-height) 0.6))
                  t)
  (set-frame-position (selected-frame)
                      (round (* (x-display-pixel-width) 0.4))
                      (round (* (x-display-pixel-height) 0.3))))

;; 啓動時frame尺寸選大
(add-hook 'window-setup-hook
          (lambda ()
            (defvar cor5-frame-size-medium nil
              "Set frame size, the default is maximum.")
            (if (eq system-type 'windows-nt)
                (cor5-cut-feet-fit-framesize)
              (toggle-frame-maximized))))

;; ;; frame大中變換
;; ;; 受`toggle-frame-maximized'影響，暫不啓用
;; (defun cor5-distort-frame-size ()
;;   "Toggle different frame size."
;;   (interactive)
;;   (if (eq cor5-frame-size-medium t)
;;       (progn
;;         (cor5-cut-feet-fit-framesize)
;;         (setq cor5-frame-size-medium nil))
;;     (progn
;;       (cor5-medium-frame-size)
;;       (setq cor5-frame-size-medium t))))

;; -------- ace-window --------
(use-package ace-window
  :preface
  (defun cor5-toggle-window-split ()
    "Toggle windows layout from up/down to left/right, vice versa."
    (interactive)
    (if (= (count-windows) 2)
        (let* ((this-win-buffer (window-buffer))
               (next-win-buffer (window-buffer (next-window)))
               (this-win-edges (window-edges (selected-window)))
               (next-win-edges (window-edges (next-window)))
               (this-win-2nd (not (and (<= (car this-win-edges)
                                           (car next-win-edges))
                                       (<= (cadr this-win-edges)
                                           (cadr next-win-edges)))))
               (splitter
                (if (= (car this-win-edges)
                       (car (window-edges (next-window))))
                    'split-window-horizontally
                  'split-window-vertically)))
          (delete-other-windows)
          (let ((first-win (selected-window)))
            (funcall splitter)
            (if this-win-2nd (other-window 1))
            (set-window-buffer (selected-window) this-win-buffer)
            (set-window-buffer (next-window) next-win-buffer)
            (select-window first-win)
            (if this-win-2nd (other-window 1))))))
  ;; :init
  ;; ;; set default jump keys, workman layout
  ;; (setq aw-keys '(97 115 104 116 103 121 110 101 111 105))
  :bind
  (("C-o" . 'other-window)
   ("<f8>" . 'hydra-ace-window/body))
  :config
  (defhydra hydra-ace-window ()
    ("[" shrink-window-horizontally "窄" :column "當前窗口變")
    ("]" enlarge-window-horizontally "寬")
    ("{" shrink-window "短")
    ("}" enlarge-window "長")
    ("=" balance-windows "均分" :column "窗口操作")
    ("-" split-window-vertically "橫斬")
    ("/" split-window-horizontally "豎劈")
    ("r" cor5-toggle-window-split "轉置" :exit t)
    ("," previous-buffer "前翻頁" :column "buffer")
    ("." next-buffer "後翻頁")
    ("s" ace-swap-window "上下左右互換")
    ("d" delete-window "關閉當前")
    ("C-d" ace-delete-window "關閉指定窗口")
    ("D" kill-buffer-and-window "關閉buffer並關窗")
    ("o" other-window "光標跳轉" :column "其他")
    ("<f8>" toggle-frame-maximized "frame大小變化" :exit t)
    ("C-g" nil "退出")
    ("q" nil "退出"))
  ;; :custom-face
  ;; (aw-leading-char-face ((t (:inherit font-lock-keyword-face :bold t :height 2.0))))
  ;; (aw-mode-line-face ((t (:inherit mode-line-emphasis :bold t))))
  :custom
  (aw-ignore-current t))

;; -------- vanilla mode-line --------
;; via: xuchengpeng etc
;; (defvar ml-selected-window nil)
;; (add-hook 'post-command-hook
;;           (lambda ()
;;             (setq ml-selected-window (selected-window))))
;; (add-hook 'buffer-list-update-hook
;;           (lambda ()
;;             (force-mode-line-update t)))
(defun cor5-buffer-encoding-abbrev ()
  "The line ending convention used in the buffer."
  (let ((buf-coding (format "%s" buffer-file-coding-system)))
    (if (string-match "\\(dos\\|gb2312\\|gbk\\|gb18030\\|unix\\|utf-8\\|mac\\)"
                      buf-coding)
        (if (string-match "\\(gb*\\)" buf-coding)
            (upcase buf-coding)
          (capitalize (match-string 1 buf-coding)))
      buf-coding)))
(setq-default mode-line-format
	          (list
	           mode-line-front-space
	           ;; '(:eval
		       ;;   (if (eq ml-selected-window (selected-window))
		       ;;       '(:eval (propertize " 😄" ;; "☻"
			   ;;  	                     'face 'font-lock-type-face))
		       ;;     '(:eval (propertize " 🙂" ;; "☺"
			   ;;                         'face 'font-lock-keyword-face))))
	           " ["
	           ;; insert vs overwrite mode, input-method in a tooltip
               '(:eval (propertize (if overwrite-mode
                                       "Ovr"
                                     "Ins")
			                       'face 'font-lock-keyword-face
                                   'help-echo (concat "Buffer is in "
			    		                              (if overwrite-mode
			    			                              "overwrite"
			    			                            "insert")
                                                      " mode")))
	           ;; was this buffer modified since the last save?
	           '(:eval (when (buffer-modified-p)
			             (concat "," (propertize "Mod"
			    		                         'face 'font-lock-warning-face
			    		                         'help-echo "Buffer has been modified"))))
	           ;; is this buffer read-only?
	           '(:eval (when buffer-read-only
			             (concat "," (propertize "RO"
			    		                         'face 'font-lock-type-face
			    		                         'help-echo "Buffer is read-only"))))
	           "] "
               ;; error message about full memory
	           (propertize "%e" 'face 'font-lock-string-face)
               ;; buffer name
	           '(:eval (propertize "%b" 'face 'font-lock-builtin-face
				                   'help-echo (buffer-file-name)))
	           " ["
               ;; line number
	           (propertize "%l" 'face 'font-lock-type-face)
	           ", "
               ;; column number from 1 (`%c' from 0)
	           (propertize "%C" 'face 'font-lock-type-face)
	           "]　(" ;; 添加全角空格，防止字體高度原因導致 mode-line 抖動。
               ;; percentage of buffer top - bottom
	           (propertize "%q" 'face 'font-lock-constant-face)
	           " / "
               ;; buffer size
	           (propertize "%I" 'face 'font-lock-constant-face)
	           ") "
               ;; buffer coding-system
               '(:eval (cor5-buffer-encoding-abbrev))
	           " "
               mode-line-modes ;; (major/minor) mode
               ;; version control information
	           '(:eval `(vc-mode vc-mode))
	           ;; " "
               ;; ;; miscellaneous information
	           ;; '(:eval mode-line-misc-info)
	           mode-line-end-spaces
	           ))

;; -------- theme --------
(use-package spacemacs-theme
  :custom
  (spacemacs-theme-comment-italic t)
  (spacemacs-theme-keyword-italic t)
  :config
  (if (equal (substring (user-login-name) 0 3) "cor")
      (load-theme 'spacemacs-dark t)
    (load-theme 'spacemacs-light t)))

(provide '+UI)

;;; +UI.el ends here
